<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once 'Log.php';
require_once 'MainController.php';
class UserImage {

    public $destination;

    public function __construct() {
        $this->destination = '';
    }

}

class Profile extends MainController {

    private $logger;

    public function __construct() {
        parent::__construct();
        $this->logger = new TS_Log(get_class());
    }

    public function index() {
        $this->load->library('form_validation');
        $data['profile'] = $this->check_isvalidated();
        $this->load->model('UserModel');
        $this->load->model('UserImageModel');
        $user_id = $this->session->userdata('user_id');
        $user = $this->UserModel->getDataByUserId($this->encrypt->decode($user_id));
        $user_image = $this->UserImageModel->getUserImageProfile($this->encrypt->decode($user_id));
        if (isset($user_image->destination)) {
            $user_image->destination = base_url($user_image->destination);
        } else {
            $user_image = new UserImage();
        }

        $user->gender = $this->getGender($user->gender);
        $user->country = $this->getCountry($user->country);
        $user->user_type = $this->getUserStatus($user->user_type);
        $user->createdate = $this->convertFormatDate($user->createdate, 'th_TH');

        $data['user'] = $user;
        $data['user_image'] = $user_image;
        $this->loadView($this, 'ProfileView', $data);
    }
    
    public function check_isvalidated() {
        $validate = $this->session->userdata('validate');
        $user_id = $this->session->userdata('user_id');
        if (isset($validate) && isset($user_id)) {
            if ($validate) {
                $this->logger->log('info', '---------------- validate : ' . $validate);
                $this->logger->log('info', '---------------- user_id : ' . $this->encrypt->decode($user_id));
                return true;
            } else {
                $this->session->sess_destroy();
                redirect(base_url());
            }
        }
    }

    public function signout() {
        $this->session->sess_destroy();
        redirect(base_url('Home'));
    }

    public function review() {
        redirect(base_url('Review'));
    }

}
